new Vue({
    el: '#app',
    data() {
        return {
            pickerOptions: pickerOptions,
            rules: {
                Start_Date: [{ required: true, message: '请选择开始时间', trigger: 'blur' }],
            },
            dealer: [],
            shop: [],
            dialogVisible: false,
            form: {
                Dealer_Unit_Price_SerialNumber: "",
                Start_Date: '',
                End_Date: '',
                Enterprise_Info_SerialNumber: $.cookie("enterprise"),
                Children: {
                    dealer_unitprice_detail: [{
                        Dealer_Unit_Price_Detail_SerialNumber: "",
                        Dealer_Unit_Price_SerialNumber: '',
                        Article_SerialNumber: '',
                        Unit_Price: '',
                        Balance_Unit_Price: '',
                        Daily_Supply: '',
                        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
                    }],
                    dealer_unitprice_storescope: [{
                        Dealer_Unit_Price_StoreScope_SerialNumber: "",
                        Dealer_Unit_Price_SerialNumber: '',
                        Dealer_SerialNumber: '',
                        Enterprise_Info_SerialNumber: $.cookie("enterprise"),
                    }],
                }
            },
            loading: false,
            tableData: [],
        }
    },
    mounted() {
        const Dealer_Unit_Price_SerialNumber = sessionStorage.getItem("Dealer_Unit_Price_SerialNumber")
        this.getData(Dealer_Unit_Price_SerialNumber)
    },
    methods: {
        getData(Primary_Key) {
            const that = this;
            $.ajax({
                type: 'POST',
                url: DomainName + "/getSubtableInquiryOriginal?token=" + $.cookie('token'),
                data: {
                    JSONObject: JSON.stringify({ dealer_unitprice: { dealer_unitprice_detail: {}, dealer_unitprice_storescope: {} } }),
                    Primary_Key,
                },
                success: function (res) {
                    console.log(res);
                    if (res.Status == 'success' && res.dealer_unitprice.length > 0) {
                        that.form = res.dealer_unitprice[0];
                    }
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
            $.ajax({
                type: 'POST',
                url: DomainName + "/getInquiryAction?token=" + $.cookie('token'),
                data: {
                    Data: "dealer_info",
                },
                success: function (res) {
                    that.dealer = res;
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                dataType: "json"
            });
            $.ajax({
                url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Data: "article_info",
                    Row_Status: "<>'2'"
                },
                success: function (res) {
                    that.shop = res
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })

        },
        Change_Unit_Price(e, i) {
            let str = this.shop.filter(i => i.Article_SerialNumber == e)[0].Unit_Price
            this.form.Children.dealer_unitprice_detail[i].Unit_Price = str
        },
        submitForm(formName) {
            const that = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    that.loading = true
                    const { add, modify } = that.deal()
                    let data = {}
                    if (add == null) {
                        data = { Data: JSON.stringify({ Modify: { dealer_unitprice: [modify] } }) }
                    } else {
                        data = { Data: JSON.stringify({ Modify: { dealer_unitprice: [modify] }, Add: { dealer_unitprice: [add], } }), }
                    }
                    $.ajax({
                        url: DomainName + '/getEditAction?token=' + $.cookie("token"),
                        type: 'POST',
                        data,
                        success: function (res) {
                            if (res.Status == 'success') {
                                that.$message.success("操作成功")
                                window.history.go(-1);
                            } else {
                                that.$message.error("操作失败，请稍后再试")
                            }
                        },
                        error: function () {
                            that.$message.error("操作失败，请稍后再试")
                        },
                        complete: function (XMLHttpRequest) {
                            that.loading = false
                            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                            }
                        }
                    })
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        deal() {
            const add = deepClone(this.form)
            const modify = deepClone(this.form)

            let add_dealer_unitprice_detail = [], modify_dealer_unitprice_detail = []
            let add_dealer_unitprice_storescope = [], modify_dealer_unitprice_storescope = []

            for (const i in modify.Children.dealer_unitprice_detail) {
                const obj = modify.Children.dealer_unitprice_detail[i];
                if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                    add_dealer_unitprice_detail.push(obj)
                } else {
                    modify_dealer_unitprice_detail.push(obj)
                }
            }
            for (const i in modify.Children.dealer_unitprice_storescope) {
                const obj = modify.Children.dealer_unitprice_storescope[i];
                if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                    add_dealer_unitprice_storescope.push(obj)
                } else {
                    modify_dealer_unitprice_storescope.push(obj)
                }
            }

            add.Children.dealer_unitprice_detail = add_dealer_unitprice_detail
            add.Children.dealer_unitprice_storescope = add_dealer_unitprice_storescope

            modify.Children.dealer_unitprice_detail = modify_dealer_unitprice_detail
            modify.Children.dealer_unitprice_storescope = modify_dealer_unitprice_storescope

            if (add_dealer_unitprice_detail.length == 0 && add_dealer_unitprice_storescope.length == 0) {
                return { add: null, modify };
            }
            return { add, modify };
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        onExcel() {
            this.dialogVisible = true
        },
        add_dealer_unitprice_storescope() {
            this.form.Children.dealer_unitprice_storescope.push({
                Dealer_Unit_Price_StoreScope_SerialNumber: "",
                Dealer_Unit_Price_SerialNumber: this.form.Dealer_Unit_Price_SerialNumber,
                Dealer_SerialNumber: '',
                Enterprise_Info_SerialNumber: $.cookie("enterprise"),
            })
        },
        remove_dealer_unitprice_storescope() {
            if (this.form.Children.dealer_unitprice_storescope.filter(item => item.Row_Status != '1').length > 0) {
                let i = this.form.Children.dealer_unitprice_storescope.filter(item => item.Row_Status != '1').length - 1
                let obj = this.form.Children.dealer_unitprice_storescope.filter(item => item.Row_Status != '1')[i]
                if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                    this.form.Children.dealer_unitprice_storescope.pop()
                } else {
                    this.$confirm('警告！此操作将删除所属经销商, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                    }).then(() => {
                        this.form.Children.dealer_unitprice_storescope.filter(item => item.Row_Status != '1')[i].Row_Status = '1'
                    }).catch(() => {
                        this.$message({
                            type: 'warning',
                            message: '已取消删除'
                        });
                    });
                }
            } else {
                this.$message({
                    message: '不能在删除了',
                    type: 'warning'
                })
            }
        },
        add_dealer_unitprice_detail() {
            this.form.Children.dealer_unitprice_detail.push({
                Dealer_Unit_Price_Detail_SerialNumber: "",
                Dealer_Unit_Price_SerialNumber:  this.form.Dealer_Unit_Price_SerialNumber,
                Article_SerialNumber: '',
                Unit_Price: '',
                Balance_Unit_Price: '',
                Daily_Supply: '',
                Enterprise_Info_SerialNumber: $.cookie("enterprise"),
            })
        },
        remove_dealer_unitprice_detail() {
            if (this.form.Children.dealer_unitprice_detail.filter(item => item.Row_Status != '1').length > 0) {
                let i = this.form.Children.dealer_unitprice_detail.filter(item => item.Row_Status != '1').length - 1
                let obj = this.form.Children.dealer_unitprice_detail.filter(item => item.Row_Status != '1')[i]
                if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                    this.form.Children.dealer_unitprice_detail.pop()
                } else {
                    this.$confirm('警告！此操作将删除该明细, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                    }).then(() => {
                        this.form.Children.dealer_unitprice_detail.filter(item => item.Row_Status != '1')[i].Row_Status = '1'
                    }).catch(() => {
                        this.$message({
                            type: 'warning',
                            message: '已取消删除'
                        });
                    });
                }
            } else {
                this.$message({
                    message: '不能在删除了',
                    type: 'warning'
                })
            }
        },
    }
})